class SurveyController < ApplicationController
        layout 'standard';
    before_filter :authorizeSurveyer, :only => [:new_survey,:create_survey,:edit_survey,:show_survey,:destroy_survey,
    											:new_rights,:create_rights]
    before_filter :authorizeSurveyee, :only => [:list_filled,:fill_survey,:createFilledSurvey,:show_filled]
  
#    before_filter :authorizeEditor, :only => [:list_filled,:fill_survey,:createFilledSurvey,:show_filled]
    

	def index									
		@surveys = Survey.find(:all, :order => :startDate)
		respond_to do |format|
			format.html # index.html.erb
			format.xml { render :xml => @surveys }
		end
	end

#------------------------------------------------------------------------------------------------
#--------------------------------------------SURVEYER--------------------------------------------

	
	
	def new_survey
	
		@survey=Survey.new
	
		@questionTypes = QuestionType.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		respond_to do |format|
			format.html 
			format.xml { render :xml => @survey }			
		end
				
	end
	
	def create_survey
		params[:survey][:userID]=session[:user_id]
		params[:survey][:creationDate]=Time.now.to_s(:db)		
		@survey = Survey.new(params[:survey]  )
		edit_id=SurveyPermissionType.find_by_name("edition").id
		stats_id=SurveyPermissionType.find_by_name("stats").id
	
		begin
		Survey.transaction do

				 @survey.save!
				 @right1=UsersSurveysPermission.new(:userID=> session[:user_id],:permissionTypeID=>edit_id,:surveyID=>@survey.id)
	   			 @right2=UsersSurveysPermission.new(:userID=> session[:user_id],:permissionTypeID=>stats_id,:surveyID=>@survey.id)		
				 @right1.save!
				 @right2.save!
	   			 redirect_to :controller=>:rights, :action => :rights_overview, :id => @survey.id,:back_forbidden=>true
		
		end
		rescue
		
			respond_to do |format|
				@questionTypes = QuestionType.find(:all, :order => "name").map {|u| [u.name, u.id] }		
				format.html { render :action => "new_survey"  }
		        format.xml  { render :xml => @survey.errors, :status => :unprocessable_entity }
	 		end
		end		
		
	end
	
	
	def edit_survey
		
		@questionTypes = QuestionType.find(:all, :order => "name").map {|u| [u.name, u.id] }		
    	@survey = Survey.find(params[:id])
    	respond_to do |format|
			format.html 
			format.xml { render :xml => @user }
		end
    	
    	
  	end
  	
  	def update_survey
	    @survey = Survey.find(params[:surveyID])	    
		begin    
	    Survey.transaction do    	
	    	@survey.update_attributes!(params[:survey])
	        redirect_to(:controller=>:survey,:action=>:list_survey_drafts)
	        
	    end 
	    rescue   
	
	      	respond_to do |format|
	    		@questionTypes = QuestionType.find(:all, :order => "name").map {|u| [u.name, u.id] }		
				format.html { render :action => "edit_survey"  }
		        format.xml  { render :xml => @survey.errors, :status => :unprocessable_entity }
	 		end
		end  	
  	end
  	
	
	def show_survey
		STDERR.puts "-----------------------------------------------------zwww"
	
		@survey = Survey.find(params[:id])
		respond_to do |format|
			format.html 
			format.xml { render :xml => @user }
		end
	end

	

	def delete_survey
		
		@survey = Survey.find(params[:id])
		@survey.destroy
	
	
		respond_to do |format|
			format.html { redirect_to(:back) }
			format.xml { head :ok }
		end
	
	end 
	

	
	def list_survey_drafts
		@surveys =  User.find(session[:user_id]).survey_drafts
		@foreignSurveys =  User.find(session[:user_id]).foreign_survey_drafts
	
		respond_to do |format|
			format.html 
			format.xml { render :xml => @surveys }
		end
	end

	def list_running
		@surveys =  User.find(session[:user_id]).running
		@foreignSurveys =  User.find(session[:user_id]).foreign_running
		respond_to do |format|
			format.html 
			format.xml { render :xml => @surveys }
		end
	end
	
	def list_finished
		@surveys =  User.find(session[:user_id]).finished
		@foreignSurveys =  User.find(session[:user_id]).foreign_finished
		respond_to do |format|
			format.html
			format.xml { render :xml => @surveys }
		end
	end
	
	def list_filled_for_surveyer
		@survey=Survey.find(params[:id])
		@filledSurveys =  @survey.filledsurveys
		respond_to do |format|
			format.html
			format.xml { render :xml => @surveys }
		end
	end
	
	def change_end_time
		@survey =  Survey.find(params[:id])
		respond_to do |format|
			format.html
			format.xml { render :xml => @surveys }
		end
	end
	
	def update_end_time
	  	@survey = Survey.find(params[:surveyID])
	    respond_to do |format|
	      if @survey.update_attributes(params[:survey])
	        flash[:notice] = 'End date was successfully updated.'
	        format.html { redirect_to(:controller=>:survey,:action=>:list_running) }
	        format.xml  { head :ok }
	      else
	        format.html	{ redirect_to(:controller=>:survey,:action=>:list_running) }
	        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
	      end
	    
  		end
	end
	
	def show_stats
		@survey =  Survey.find(params[:id])
		respond_to do |format|
			format.html
			format.xml { render :xml => @surveys }
		end
	end

#------------------------------------------------------------------------------------------------
#--------------------------------------------SURVEYEE--------------------------------------------
	
	def list_filled
		
		@filledSurveys = FilledSurvey.find(:all, :order => :date)
		respond_to do |format|
			format.html # index.html.erb
			format.xml { render :xml => @surveys }
		end
		
	end
	
	def show_filled_survey
		
		
		@filled = FilledSurvey.find(params[:id])
		respond_to do |format|
			format.html 
			format.xml { render :xml => @user }
		end
		
	end

	
        
	def fill_survey
		@survey = Survey.find(params[:id])
		@filledSurvey = @survey.filledsurveys.build
	
		respond_to do |format|
				format.html # fill.html.erb
				format.xml { render :xml => @user }
		end
	
	end 
	
	def createFilledSurvey
		params[:filled_survey][:userID]=session[:user_id]
		params[:filled_survey][:creationDate]=Time.now

		
		@filledSurvey = FilledSurvey.new(params[:filled_survey])
		
	begin
		FilledSurvey.transaction do
			
			@filledSurvey.save!
			if params[:closedAnswers]!=nil
			for cq in params[:closedAnswers]						
					@right=CQAnswer.new(:possibleAnswerID=> cq[0],:filledSurveyID=>@filledSurvey.id)
					@right.save! 	
			end
			end
			if @filledSurvey.valid?
				redirect_to(:controller=>:survey,:action=>:list_available_to_fill)
			else
				raise 
			end
		end
	rescue
	      	respond_to do |format|
	      		@survey = Survey.find(params[:filled_survey][:surveyID])
				format.html { render :action => "fill_survey"  }
		        format.xml  { render :xml => @filledSurvey.errors, :status => :unprocessable_entity }
	 		end
 		end
	end
	
	def edit_filled_survey
		
    	@filledSurvey = FilledSurvey.find(params[:id])
    	respond_to do |format|
			format.html 
			format.xml { render :xml => @user }
		end
    	
    	
  	end
  	
  	def update_filled_survey

	    @filledSurvey = FilledSurvey.find(params[:filledSurveyID])
	begin
	    FilledSurvey.transaction do
		    if params[:existing]!=nil
				for cqanswer in @filledSurvey.cqanswers
							
					if (params[:existing][cqanswer.id.to_s]==nil)
							CQAnswer.delete(cqanswer.id)
					end
					
				end
				
			else	
				CQAnswer.delete_all("FILLED_SURVEY_ID=#{params[:filledSurveyID]}" )
		 	end
		    	if params[:new]!=nil
				for newanswer in params[:new]
						 	@possibleAnswerID=newanswer[0]
							@answer=CQAnswer.new(:possibleAnswerID=>@possibleAnswerID,:filledSurveyID=>params[:filledSurveyID])
							@answer.save!	
				end
			end
			@filledSurvey.update_attributes!(params[:filled_survey])
			redirect_to(:controller=>:survey,:action=>:list_filled_incompletely) 
		end
	rescue
	    respond_to do |format|
		format.html { render :action => "edit_filled_survey"  }
		        format.xml  { render :xml => @filledSurvey.errors, :status => :unprocessable_entity }
	    
  		end
  	end
    	
  	end
  	
	
	def delete_filled_survey
		@filledSurvey = FilledSurvey.find(params[:id])
		@filledSurvey.destroy
		respond_to do |format|
			format.html { redirect_to(:controller=>:main,:action=>:index) }
			format.xml { head :ok }
		end
		
	end
	
	def list_available_to_fill
		@surveys =  User.find(session[:user_id]).available_to_fill
		respond_to do |format|
			format.html 
			format.xml { render :xml => @surveys }
		end
	end
	def list_filled_incompletely
		@surveys =  User.find(session[:user_id]).filled_incompletely
		respond_to do |format|
			format.html 
			format.xml { render :xml => @surveys }
		end
	end
	def list_filled_completely
		@surveys =  User.find(session[:user_id]).filled_completely
		respond_to do |format|
			format.html
			format.xml { render :xml => @surveys }
		end
	end
	




end
